package com.study.rocketmq.controller;

import com.alibaba.fastjson.JSONObject;
import com.study.rocketmq.constant.MQTopicConstant;
import com.study.rocketmq.message.MQMessage;
import com.study.rocketmq.model.User;
import com.study.rocketmq.service.IRocketMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RockermqController {

    @Autowired
    private IRocketMQService rocketMQService;

    @PostMapping("/send")
    public boolean send(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        String content = jsonObject.toJSONString(user);
        MQMessage message = new MQMessage(MQTopicConstant.DEMO_TOPIC,MQTopicConstant.DEMO_TAGS,content);
        return rocketMQService.send(message);

    }

    @PostMapping("/asyncSend")
    public boolean asyncSend(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        String content = jsonObject.toJSONString(user);
        MQMessage message = new MQMessage(MQTopicConstant.DEMO_TOPIC,MQTopicConstant.DEMO_TAGS,content);
        rocketMQService.asyncSend(message);
        return true;
    }

    @PostMapping("/syncSendOrderly")
    public boolean syncSendOrderly(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        String content = jsonObject.toJSONString(user);
        MQMessage message = new MQMessage(MQTopicConstant.DEMO_TOPIC,MQTopicConstant.DEMO_TAGS,content);
        rocketMQService.syncSendOrderly(message);
        return true;
    }

}
